package com.guoqi.keepsign.base

import android.content.Context
import android.graphics.Bitmap
import android.support.v4.app.Fragment
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.Headers
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.guoqi.keepsign.R
import com.guoqi.keepsign.previewlibrary.loader.IZoomMediaLoader
import com.guoqi.keepsign.previewlibrary.loader.MySimpleTarget

class MyImageLoader : IZoomMediaLoader {

    override fun displayImage(context: Fragment, url: String, imageView: ImageView, simpleTarget: MySimpleTarget) {
        val glideUrl = GlideUrl(url, Headers {
            val header = HashMap<String, String>()
            header["cookie"] = "Hm_lvt_dbc355aef238b6c32b43eacbbf161c3c=1536981553; Hm_lpvt_dbc355aef238b6c32b43eacbbf161c3c=1536986863"
            header["Referer"] = url
            header
        })
        Glide.with(context).load(glideUrl)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.mipmap.ic_image_loading)
                .listener(object : RequestListener<GlideUrl, Bitmap> {
                    override fun onException(e: java.lang.Exception?, model: GlideUrl?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                        simpleTarget.onLoadFailed(null)
                        return false
                    }

                    override fun onResourceReady(resource: Bitmap?, model: GlideUrl?, target: Target<Bitmap>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
                        simpleTarget.onResourceReady()
                        return false
                    }
                })
                .into(imageView)
    }


    override fun displayGifImage(context: Fragment, path: String, imageView: ImageView, simpleTarget: MySimpleTarget) {
        Glide.with(context).load(path)
                .asGif()
                //可以解决gif比较几种时 ，加载过慢  //DiskCacheStrategy.NONE
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.mipmap.ic_image_loading)
                .listener(object : RequestListener<String, GifDrawable> {
                    override fun onException(e: Exception, model: String, target: Target<GifDrawable>, isFirstResource: Boolean): Boolean {
                        simpleTarget.onResourceReady()
                        return false
                    }

                    override fun onResourceReady(resource: GifDrawable, model: String, target: Target<GifDrawable>, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
                        simpleTarget.onLoadFailed(null)
                        return false
                    }
                })
                .into(imageView)
    }

    override fun onStop(context: Fragment) {
        Glide.with(context).onStop()
    }

    override fun clearMemory(c: Context) {
        Glide.get(c).clearMemory()
    }


}

